<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace app\api\controller\v1;

use think\Db;
use think\Request;

class Shop extends Base
{
    public $users;
    public $users_id;

    /**
     * 初始化操作
     */
    public function _initialize()
    {
        parent::_initialize();
        $paramA = input('param.a');
        if (!empty($paramA) && !in_array($paramA, ['eyou_cart_list', 'eyou_order_list'])) {
            $this->users = $this->getUser();
            $this->users_id = !empty($this->users['users_id']) ? intval($this->users['users_id']) : null;
            if (empty($this->users_id)) $this->error('请先登录');
        } else {
            $this->users = $this->getUser(false);
            $this->users_id = !empty($this->users['users_id']) ? intval($this->users['users_id']) : null;
        }
        // 商城模型
        $this->shopModel = model('v1.Shop');
        // 商城购物车模型
        $this->shopCartModel = model('v1.ShopCart');
        // 商城订单模型
        $this->shopOrderModel = model('v1.ShopOrder');
        // 商城地址模型
        $this->shopAddressModel = model('v1.ShopAddress');
        // 商城订单维权模型
        $this->shopOrderServiceModel = model('v1.ShopOrderService');
        // 积分商城订单模型
        $this->ScoreShopModel = model('v1.ScoreShop');
    }

    // 立即购买(商品详情页直接购买)
    public function eyou_goods_buy_now()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // 数量判断
            if (empty($post['product_num']) || 0 > $post['product_num']) $this->error('请输入正确数量');

            // 立即购买(商品详情页直接购买)
            $this->shopOrderModel->getGoodsBuyNow($post);
        }
    }

    // 订单提交页显示数据
    public function eyou_goods_buy_page()
    {
        if (IS_AJAX_POST) {
            $cookieID = input('param.cookieID/s', '');
            $cartSubmit = input('param.cartSubmit/d', 0);
            if (empty($cookieID) && empty($cartSubmit)) $this->error('无效链接！');
            // 获取商品信息进行展示
            $this->shopOrderModel->getGoodsBuyPage($cookieID, $this->users);
        }
    }

    // 订单支付
    public function eyou_order_pay()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            $post['action'] = !empty($post['action']) ? $post['action'] : 'createPay';

            // 操作分发
            if ('directPay' == $post['action']) {
                // 订单直接支付
                $this->shopOrderModel->getOrderDirectPay($post, $this->users);
            } else if ('createPay' == $post['action']) {
                // 获取解析数据
                if (empty($post['cookieID']) && empty($post['cartSubmit'])) $this->error('无效链接！');
                // 获取商品信息生成订单并支付
                $this->shopOrderModel->getOrderPay($post, $this->users);
            }
        }
    }

    // 订单支付后续处理
    public function eyou_order_pay_handle()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 订单支付后续处理
            $this->shopOrderModel->getOrderPayHandle($post, $this->users);
        }
    }

    // 购物车列表
    public function eyou_cart_list()
    {
        if (IS_AJAX) {
            // 获取商品信息生成订单并支付
            $levelDiscount = !empty($this->users['level_discount']) ? $this->users['level_discount'] : 0;
            $result = $this->shopCartModel->getCartList($levelDiscount);

            // 标签数据
            $data = $this->apiLogic->taglibData($this->users);
            if (!empty($data)) $result = array_merge($result, $data);

            // 返回数据
            $this->renderSuccess($result);
        }
    }

    // 购物车操作(集合方法)
    public function eyou_cart_action()
    {
        if (IS_AJAX) {
            $param = input('param.');
            if (empty($param['action'])) $param['action'] = null;

            // 判断执行操作
            if ('addCart' == $param['action']) {
                // 添加购物车商品
                $this->shopCartModel->addCartGoods($param);
            } else if ('delCart' == $param['action']) {
                // 删除购物车商品
                $this->shopCartModel->delCartGoods($param);
            } else if ('number' == $param['action']) {
                // 购物车数量操作
                $this->shopCartModel->updateCartGoodsNumber($param);
            } else if ('selected' == $param['action']) {
                // 是否选中
                $this->shopCartModel->updateCartSelected($param);
            } else if ('selectedAll' == $param['action']) {
                // 是否全部选中
                $this->shopCartModel->updateCartAllSelected($param);
            } else if ('remove' == $param['action']) {
                // 移除购物车商品
                $this->shopCartModel->removeCartGoods($param);
            } else if ('abnormal' == $param['action']) {
                // 删除购物车商品
                $this->shopCartModel->abnormalCartGoods($param);
            } else {
                $this->error('请正确操作');
            }
        }
    }

    // 我的订单列表
    public function eyou_order_list($dataType = 'all')
    {
        $list = $this->shopOrderModel->getOrderList($dataType);
        return $this->renderSuccess($list);
    }
    /**
     * 获取评价订单商品列表
     * @param $order_id
     * @return array
     * @throws \Exception
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function order_comment($order_id)
    {
        if (IS_AJAX) {
            $data = model('v1.Shop')->getOrderComment($order_id, $this->users_id);
            if (!empty($data)){
                foreach ($data as $k => $v){
                    if (1 == $v['apply_service']){
                        unset($data[$k]);
                    }
                }
            }
            $detail['goods'] = array_merge($data);
            return $this->renderSuccess([
                'detail' => $detail,
            ]);
        }
        $this->error('读取失败！');
    }


    // 订单详情信息
    public function eyou_order_detail($order_id = 0)
    {
        if (IS_AJAX) {
            // 订单详情
            $detail = $this->shopOrderModel->getOrderDetail($order_id, $this->users_id);
            $times = getTime();
            $config = getUsersConfigData('order');
            if (!empty($config['order_unpay_close_time'])) {
                // 计算下单过期时间秒数
                $config['order_unpay_close_time'] = ((intval($config['order_unpay_close_time']) * 60) + intval($detail['add_time'])) - intval($times);
            } else {
                $config['order_unpay_close_time'] = 0;
            }
            if (!empty($config['order_auto_receipt_time'])) {
                // 计算自动确认收货时间秒数
                $config['order_auto_receipt_time'] = ((intval($config['order_auto_receipt_time']) * 86400) + intval($detail['express_time'])) - intval($times);
            } else {
                $config['order_auto_receipt_time'] = 0;
            }
            if (!empty($config['order_right_protect_time'])) {
                // 计算收货后可维权时间
                $config['order_right_protect_time'] = ((intval($config['order_right_protect_time']) * 86400) + intval($detail['confirm_time'])) - intval($times);
            } else {
                $config['order_right_protect_time'] = 0;
            }
            if (!empty($detail['order_status']) && -1 === intval($detail['order_status'])) $detail['order_status'] = 4;

            // 是否开启订单评价功能
            $config['shop_open_comment'] = !empty($this->usersConfig['shop_open_comment']) ? intval($this->usersConfig['shop_open_comment']) : 0;
            $score = getUsersConfigData('score');
            $config['score_name'] = $score['score_name'];

            return $this->renderSuccess([
                'config' => $config,
                'detail' => $detail,
            ]);
        }
        $this->error('订单读取失败！');
    }

    // 订单操作(集合方法)
    public function eyou_order_action()
    {
        if (IS_AJAX) {
            $param = input('param.');
            if (empty($param['action'])) $param['action'] = null;

            // 判断执行操作
            if ('cancel' == $param['action']) {
                // 取消订单，更新订单为已取消状态
                $this->shopOrderModel->cancelOrder($param);
            } else if ('againBuy' == $param['action']) {
                // 重新购买订单商品
                $this->shopOrderModel->againBuyOrder($param);
            } else if ('remind' == $param['action']) {
                // 提醒订单发货
                $this->shopOrderModel->remindOrderSend($param);
            } else if ('confirm' == $param['action']) {
                // 确认订单已收货
                $this->shopOrderModel->confirmOrder($param);
            } else if ('deleteFind' == $param['action']) {
                // 删除订单信息
                $this->shopOrderModel->deleteOrder($param);
            } else {
                $this->error('请正确操作');
            }
        }
    }

    // 处理订单维权(集合方法)
    public function eyou_order_service_action()
    {
        if (IS_AJAX) {
            // 传入参数
            $post = input('post.');

            // 处理传入的参数
            $post['order_id'] = !empty($post['order_id']) ? intval($post['order_id']) : 0;
            $post['product_id'] = !empty($post['product_id']) ? intval($post['product_id']) : 0;
            $post['details_id'] = !empty($post['details_id']) ? intval($post['details_id']) : 0;
            $post['service_id'] = !empty($post['service_id']) ? intval($post['service_id']) : 0;

            // 判断执行操作
            $action = !empty($post['action']) ? strval($post['action']) : '';
            if ('goodsDetect' == $action) {
                // 查询订单内的指定的某个商品，判断是否已申请售后或已评价商品
                $this->shopOrderServiceModel->getOrderGoodsDetect($post);
            } else if ('getServiceList' == $action) {
                // 查询售后服务单列表
                $this->shopOrderServiceModel->getOrderGoodsServiceList($post);
            } else if ('getService' == $action) {
                // 查询申请售后的订单商品
                $this->shopOrderServiceModel->getOrderGoodsService($post);
            } else if ('addService' == $action) {
                // 添加售后订单
                $this->shopOrderServiceModel->addOrderGoodsService($post);
            } else if ('cancelService' == $action) {
                // 取消售后订单
                $this->shopOrderServiceModel->cancelOrderGoodsService($post);
            } else if ('addUsersDelivery' == $action) {
                // 添加售后订单会员邮寄物流信息
                $this->shopOrderServiceModel->addServiceUsersDelivery($post);
            } else if ('confirmService' == $action) {
                // 买家确认收到商品
                $this->shopOrderServiceModel->confirmOrderGoodsService($post);
            } else {
                $this->error('请正确操作');
            }
        }
    }

    // 收货地址列表
    public function eyou_address_list()
    {
        if (IS_AJAX) {
            // 收货地址列表
            $this->shopAddressModel->getAddressList($this->users);
        }
    }

    // 收货地址操作分发
    public function shop_address_action()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');

            // 判断执行操作
            if ('find_add' == $post['action']) {
                // 添加单条收货地址
                $this->shopAddressModel->FindAddAddr($post, $this->users_id);
            } else if ('find_edit' == $post['action']) {
                // 编辑收货地址
                $this->shopAddressModel->FindEditAddr($post, $this->users_id);
            } else if ('default' == $post['action']) {
                // 设置默认收货地址
                $this->shopAddressModel->SetDefaultAddr($post, $this->users_id);
            } else if ('find_detail' == $post['action']) {
                // 获取单条收货地址
                $this->shopAddressModel->GetFindAddrDetail($post, $this->users);
            } else if ('find_del' == $post['action']) {
                // 删除单条收货地址
                $this->shopAddressModel->FindDelAddr($post, $this->users_id);
            } else {
                $this->error('请正确操作');
            }
        }
    }

    // 立即购买(商品详情页直接购买)
    public function eyou_score_exchange_now()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            // 数量判断
            if (empty($post['product_num']) || 0 > $post['product_num']) $this->error('请输入正确数量');

            $this->ScoreShopModel->getGoodsUrl($post);
        }
    }

    //积分兑换提交页显示数据
    public function eyou_score_exchange_buy_page()
    {
        if (IS_AJAX_POST) {
            $cookieID = input('param.cookieID/s', '');
            if (empty($cookieID)) $this->error('无效链接！');
            // 获取商品信息进行展示
            $this->ScoreShopModel->getGoodsBuyPage($cookieID, $this->users);
        }
    }

    // 积分商城订单支付
    public function eyou_score_order_pay()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            $post['action'] = !empty($post['action']) ? $post['action'] : 'createPay';

            // 操作分发
            if ('directPay' == $post['action']) {
               // 订单直接支付
               // $this->shopOrderModel->getOrderDirectPay($post, $this->users);
            } else if ('createPay' == $post['action']) {
                // 获取解析数据
                if (empty($post['cookieID'])) $this->error('无效链接！');
                // 获取商品信息生成订单并支付
                $this->ScoreShopModel->getScoreOrderPay($post, $this->users);
            }
        }
    }
}